import 'package:flutter/material.dart';
import 'package:english_words/english_words.dart';
import 'layout/layout.dart';
import 'layout/ContainerEx.dart';
import 'layout/StackEx.dart';
import 'layout/StateEx.dart';
import 'layout/ShoppingEx.dart';
import 'layout/BottomNavBarEx.dart';
import 'layout/DefaultTabControllerEx.dart';
import 'layout/TabControllerEx.dart';
import 'layout/AppBarBottomEx.dart';
import 'layout/DrawerEx.dart';
import 'layout/FloatingActionBarEx.dart';
import 'layout/PopupMenuEx.dart';
import 'layout/ButtonBarEx.dart';
import 'layout/TextfieldEx.dart';
import 'layout/SelectBoxEx.dart';
import 'layout/PopEx.dart';
import 'layout/ContentShowEx.dart';
import 'layout/CustomListTileEx.dart';
import 'layout/StepperEx.dart';
import 'layout/LayoutEx.dart';
import 'layout/FlowMenu.dart';
import 'layout/FormEx.dart';
import 'layout/AnimationEx.dart';
import 'layout/InteractionEx.dart';
import 'layout/DraggerEx.dart';
import 'layout/EffectsEx.dart';
import 'layout/AsyncEx.dart';
import 'layout/ScrollEx.dart';
import 'layout/ThemeEx.dart';
import 'cookbook/CookBook.dart';
import 'example/ExampleEx.dart';
void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: "FIRST APP",
      theme: ThemeData(
        accentColor: Colors.red,
        primaryColor: Colors.green
      ),
      initialRoute: '/',
      routes: <String, WidgetBuilder>
      {
        '/':(BuildContext context) => LearnListStatfulPage(),
        '/layout':(BuildContext context) => LayoutWidget(),
        '/container':(BuildContext context) => ContainerEx(),
        '/stack':(BuildContext context) => StackEx(),
        '/state':(BuildContext context) => StateEx(),
        '/shopping':(BuildContext context) => ShoppingEx(),
        '/bottomnavbar':(BuildContext context) => BottomNavBarEx(),
        '/defaultTabController':(BuildContext context) => DefaultTabControllerEx(),
        '/tabcontroller':(BuildContext context) => TabControllerEx(),
        '/appbarbottom':(BuildContext context) => AppBarBottomSample(),
        '/drawer':(BuildContext context) => DrawerEx(),
        '/floatingactionbar':(BuildContext context) => FloatingActionBarEx(),
        '/popupmenu':(BuildContext context) => PopupMenuEx(),
        '/buttonbar':(BuildContext context) => ButtonBarEx(),
        '/textfield':(BuildContext context) => TextfieldEx(),
        '/selectbox':(BuildContext context) => SelectBoxEx(),
        '/pop':(BuildContext context) => PopEx(),
        '/contentshow':(BuildContext context)  => ContentShowEx(),
        '/customlisttile':(BuildContext context) => CustomListTileEx(),
        '/stepper':(BuildContext context) => StepperEx(),
        '/layoutex':(BuildContext context) => LayoutEx(),
        '/flowmenu':(BuildContext context) => FlowMenu(),
        '/formEx':(BuildContext context) => FormEx(),
        '/animation':(BuildContext context) => AnimationEx(),
        '/interaction':(BuildContext context) => InteractionEx(),
        '/dragger':(BuildContext context) => DraggerEx(),
        '/effects':(BuildContext context) => EffectsEx(),
        '/async':(BuildContext context) => AsyncEx(),
        '/scroll':(BuildContext context) => ScrollEx(),
        '/theme':(BuildContext context) => ThemeEx(),
        '/cookbook':(BuildContext context) => CookBook(),
        '/example':(BuildContext context) => ExampleEx(),
      },
    );
  }
}


class LearnListStatfulPage extends StatefulWidget
{
  @override
  State<StatefulWidget> createState() {
    return LearnListState();
  }
}

class LearnListState extends State<LearnListStatfulPage>
{
  final _save = <WordPair>[];
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Flutter"),
        actions: [IconButton(icon: Icon(Icons.list), onPressed: _onPush)],
      ),
      body: ListView(
        children: [
          _buildRow("布局练习", 0),
          Divider(),
          _buildRow("Container示例", 1),
          Divider(),
          _buildRow("Stack示例", 2),
          Divider(),
          _buildRow("State示例", 3),
          Divider(),
          _buildRow("Shopping示例", 4),
          Divider(),
          _buildRow("bottomnavbar 示例", 5),
          Divider(),
          _buildRow('DefaultTabController 示例', 6),
          Divider(),
          _buildRow('tabcontroller 示例', 7),
          Divider(),
          _buildRow('appbarbottom 示例', 8),
          Divider(),
          _buildRow('drawer 示例', 9),
          Divider(),
          _buildRow('floatingactionbar 示例', 10),
          Divider(),
          _buildRow('popupmenu  示例', 11),
          Divider(),
          _buildRow('button bar  示例', 12),
          Divider(),
          _buildRow('textfield  示例', 13),
          Divider(),
          _buildRow('选择框相关 示例', 14),
          Divider(),
          _buildRow('弹窗相关  示例', 15),
          Divider(),
          _buildRow('信息展示  示例', 16),
          Divider(),
          _buildRow('自定义listtile  示例', 17),
          Divider(),
          _buildRow('Stepper  示例', 18),
          Divider(),
          _buildRow('布局widget  示例', 19),
          Divider(),
          _buildRow('Flow流布局  示例', 20),
          Divider(),
          _buildRow('Form表单 示例', 21),
          Divider(),
          _buildRow('动画 示例', 22),
          Divider(),
          _buildRow('交互模型 示例', 23),
          Divider(),
          _buildRow('拖动 示例', 24),
          Divider(),
          _buildRow('绘制与视觉效果 示例', 25),
          Divider(),
          _buildRow('Async异步 测试', 26),
          Divider(),
          _buildRow('滚动 测试', 27),
          Divider(),
          _buildRow('主题 测试', 28),
          Divider(),
          _buildRow('CookBook 测试', 29),
          Divider(),
          _buildRow('示例目录', 30),
        ],
      ),

    );
  }

  Widget _buildRow(String wordPair, index)
  {
    return ListTile(
      title: Text(wordPair),
      trailing: Icon(Icons.arrow_forward_ios),
      onTap: (){
        jumpToWidget(wordPair, index);
      },
    );
  }
  void jumpToWidget(String wordPair, index)
  {
    if(index == 0) {
      Navigator.pushNamed(context, '/layout');
    }else if(index == 1){
      Navigator.pushNamed(context, '/container');
    }else if(index == 2){
      Navigator.pushNamed(context, '/stack');
    }else if(index == 3){
      Navigator.pushNamed(context, '/state');
    }else if(index == 4){
      Navigator.pushNamed(context, '/shopping');
    }else if(index == 5){
      Navigator.pushNamed(context, '/bottomnavbar');
    }else if(index == 6){
      Navigator.pushNamed(context, '/defaultTabController');
    }else if(index == 7){
      Navigator.pushNamed(context, '/tabcontroller');
    }else if(index == 8){
      Navigator.pushNamed(context, '/appbarbottom');
    }else if(index == 9){
      Navigator.pushNamed(context, '/drawer');
    }else if(index == 10){
      Navigator.pushNamed(context, '/floatingactionbar');
    }else if(index == 11){
      Navigator.pushNamed(context, '/popupmenu');
    }else if(index == 12){
      Navigator.pushNamed(context, '/buttonbar');
    }else if(index == 13){
      Navigator.pushNamed(context, '/textfield');
    }else if(index == 14){
      Navigator.pushNamed(context, '/selectbox');
    }else if(index == 15){
      Navigator.pushNamed(context, '/pop');
    }else if(index == 16){
      Navigator.pushNamed(context, '/contentshow');
    }else if(index == 17){
      Navigator.pushNamed(context, '/customlisttile');
    }else if(index == 18){
      Navigator.pushNamed(context, '/stepper');
    }else if(index == 19){
      Navigator.pushNamed(context, '/layoutex');
    }else if(index == 20){
      Navigator.pushNamed(context, '/flowmenu');
    }else if(index == 21){
      Navigator.pushNamed(context, '/formEx');
    }else if(index == 22){
      Navigator.pushNamed(context, '/animation');
    }else if(index == 23){
      Navigator.pushNamed(context, '/interaction');
    }else if(index == 24){
      Navigator.pushNamed(context, '/dragger');
    }else if(index == 25){
      Navigator.pushNamed(context, '/effects');
    }else if(index == 26){
      Navigator.pushNamed(context, '/async');
    }else if(index == 27){
      Navigator.pushNamed(context, '/scroll');
    }else if(index == 28){
      Navigator.pushNamed(context, '/theme');
    }else if(index == 29){
      Navigator.pushNamed(context, '/cookbook');
    }else if(index == 30){
      Navigator.pushNamed(context, '/example');
    }
  }
  void _onPush()
  {
    Navigator.of(context).push(
        MaterialPageRoute(builder: (context){
          final tiles = _save.map((wordpair) => ListTile(title: Text(wordpair.asPascalCase),
            onTap: (){
              Navigator.of(context).pop();
            },
          ));
          print('datas is empty 0 $tiles');
          if(tiles.isNotEmpty) {
            final Iterable<Widget> data1s = ListTile.divideTiles(
                context: context,
                tiles: tiles
            );
            print('datas is empty 0 $data1s');
            if (data1s == null) {
              print('datas is empty 00');
            } else {
              print('datas is empty 000');
            }
          }
          // data1s.toList();
          // print('datas is empty 1');
          // if(data1s.isEmpty)
          //   {
          //     print('datas is empty');
          //   }else {
          //   print('datas.length = ${data1s.length}');
          // }
          return Scaffold(
            appBar: AppBar(
                title: Text('Navigator 练习')
            ),
            body:Center(
              child: Text('Navigator跳转成功'),
            ),
          );
        })
    );
  }
}






class MyHomePage extends StatelessWidget
{
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("first flutter app"),
        toolbarTextStyle: TextStyle(fontSize: 22),
      ),
      body: EnglishListStatfulPage()
    );
  }
}

class EnglishListPage extends StatelessWidget
{
  final _englishWords = <WordPair>[];
  @override
  Widget build(BuildContext context) {
    return ListView.builder(
        itemBuilder: (context, index){
          if(index.isOdd) return Divider();
          int row = index ~/ 2;
          if(row >= _englishWords.length)
            {
              _englishWords.addAll(generateWordPairs().take(10));
            }
          return _buildRow(_englishWords[row]);
        },
    );
  }

  Widget _buildRow(WordPair wordPair)
  {
    return ListTile(
      title: Text(wordPair.asPascalCase),
    );
  }
}

class EnglishListStatfulPage extends StatefulWidget
{
  @override
  State<StatefulWidget> createState() {
    return EnglishListState();
  }
}

class EnglishListState extends State<EnglishListStatfulPage>
{
  final _wordList = <WordPair>[];
  final _save = <WordPair>[];
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("words list"),
        actions: [IconButton(icon: Icon(Icons.list), onPressed: _onPush)],
      ),
      body: ListView.builder(
          itemBuilder: (context, i){
            if(i.isOdd) return new Divider();

            int index = i ~/ 2;
            if(index >= _wordList.length)
            {
              _wordList.addAll(generateWordPairs().take(10));
            }
            return _buildRow(_wordList[index]);
          }
      ),
      
    );
  }

  Widget _buildRow(WordPair wordPair)
  {
    final aleardySave = _save.contains(wordPair);
    return ListTile(
      title: Text(wordPair.asPascalCase),
      trailing: Icon(aleardySave ? Icons.favorite : Icons.favorite_border,
        color: aleardySave? Colors.red : null,
      ),
      onTap: (){
        setState(() {
          if(_save.contains(wordPair))
          {
            _save.remove(wordPair);
          }else
          {
            _save.add(wordPair);
          }
        });
      },
    );
  }

  void _onPush()
  {
    Navigator.of(context).push(
      MaterialPageRoute(builder: (context){
        final tiles = _save.map((wordpair) => ListTile(title: Text(wordpair.asPascalCase),
          onTap: (){
            Navigator.of(context).pop();
          },
        ));
        final datas =  ListTile.divideTiles(context: context,
            tiles: tiles
        ).toList();
        return Scaffold(
          appBar: AppBar(
            title: Text('save data list')
          ),
          body: ListView(
            children: datas,
          ),
        );
      })
    );
  }
}

